package tool.softT

import dao._
import implicits.Implicits._
import play.api.i18n.Messages
import shared.Pojo._
import tool.Pojo._
import tool.{Tool, _}
import tool.plot._

import java.io.File

/** Created by yz on 7/12/2020
  */
trait PcaSoftBT extends AllSoftBT with PcaSoftT {

  override def getFileInfos(workspaceDir: File, index: Int)(implicit
      matrixFileDataDao: MatrixFileDataDao,
      messages: Messages
  ): List[FileInfoData] = {
    val subWorkspaceDir = new File(workspaceDir, s"${nameStr}_${index}")
    val boxplotFile = new File(subWorkspaceDir, "PCA_Score_with_Boxplot_with_Points.pdf")
    val boxplotData = if (boxplotFile.exists()) {
      List(
        FileInfoData("PCA_Score_with_Boxplot_with_Points.pdf", messages("pCAScoreWithBoxplotWithPoints"))
      )
    } else List.empty
    List(
      FileInfoData("PCA_Score.csv", messages("pCAScore")),
      FileInfoData("PCA_R2.csv", messages("pCAR2")),
      FileInfoData("PCA_Score_2D.pdf", messages("pCAScore2D")),
      FileInfoData("PCA_Score_3D.pdf", messages("pCAScore3D")),
      FileInfoData("PCA_Screeplot.pdf", messages("pCAScreeplot"))
    ) ::: boxplotData
  }

  override def getCommandData(workspaceDir: File, resultDir: File, index: Int): List[CommandData] = {
    val rawDataFile = new File(workspaceDir, "AllMet_Raw.txt")
    val groupFile = new File(workspaceDir, "group.txt")
    val solutionFile = new File(Tool.dataDir, "color_solution/color_solution_1.txt")
    val command =
      s"""
         |${Tool.rScript} ${new File(Tool.rPath, "produce_sample_color.R").unixPath} --g ${groupFile.unixPath}  --solution ${solutionFile.unixPath}
         |${Tool.rScript} ${Tool.rPath.unixPath}/fill_sample_color.R --g ${groupFile.unixPath}  --solution ${solutionFile.unixPath} --pc ${plot.Pca2D.plotO.configFileName}
         |${Tool.rScript} ${Tool.rPath.unixPath}/fill_sample_color.R --g ${groupFile.unixPath}  --solution ${solutionFile.unixPath} --pc ${plot.Pca3D.plotO.configFileName}
         |${Tool.rScript} ${Tool.rPath.unixPath}/fill_sample_color.R --g ${groupFile.unixPath}  --solution ${solutionFile.unixPath} --pc ${plot.PcaBoxplot.plotO.configFileName}
         |${Tool.rScript} ${new File(Tool.rPath, "pca_calculate.R").unixPath} --i ${rawDataFile.unixPath}
        """.stripMargin
    val plotCommands =List(
      plot.Pca2D.getCommand(workspaceDir,""),
      plot.Pca3D.getCommand(workspaceDir,""),
      plot.Variance.getCommand(workspaceDir,""),
    )
    val boxCommand =plot.PcaBoxplot.getCommand(workspaceDir,"")
    val commands = if (Tool.isSingleGroup(groupFile)) {
      List(command)
    } else {
      List(command, boxCommand)
    }
    List(CommandData(workspaceDir, commands:::plotCommands))
  }

  override def producePlotConfigFile(workspaceDir: File, index: Int): Any = {
    plot.Pca2D.initConfigFile(workspaceDir, idStr = "")
    plot.Pca3D.initConfigFile(workspaceDir, idStr = "")
    plot.Variance.initConfigFile(workspaceDir, idStr = "")
    plot.PcaBoxplot.initConfigFile(workspaceDir, idStr = "")
  }

}
